package com.bosssoft.ct.redis.demo1.config;

import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;

import javax.sql.DataSource;

/**
 * @author ChenTong
 * @version 1.0
 * @description MyBatis设置
 * @date 2020/6/18
 **/
@Configuration
@MapperScan("com.bosssoft.ct.redis.demo1.**.dao") // Spring不知道dao在哪里 这里进行手动扫描
public class MybatisConfig {
    @Autowired
    private DataSource dataSource;

    @Bean
    public SqlSessionFactory sqlSessionFactory() {
        SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();
        sessionFactory.setDataSource(dataSource);
        sessionFactory.setTypeAliasesPackage("com.bosssoft.ct.redis.demo1.entity");    // 扫描Model

        PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
        sessionFactory.setMapperLocations(resolver.getResources("classpath:**/sqlmap/*.xml"));    // 扫描映射文件

        return sessionFactory.getObject();
    }

}